package com.b2.service.impl;

import com.b2.constant.MessageConstant;
import com.b2.context.BaseContext;
import com.b2.mapper.ClassMapper;
import com.b2.mapper.TeacherMapper;
import com.b2.pojo.dto.addTeacherDto;
import com.b2.pojo.entity.Class;
import com.b2.pojo.entity.Teacher;
import com.b2.service.TeacherService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* @author 悦神
* @description 针对表【teacher】的数据库操作Service实现
* @createDate 2024-04-25 14:10:59
*/
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher>
    implements TeacherService{
    @Autowired
    ClassMapper classMapper;
    @Autowired
    TeacherMapper teacherMapper;
    @Override
    @Transactional
    public void addteacher(addTeacherDto teacher) {
        String code = teacher.getCode();
        LambdaQueryWrapper<Class> o = new LambdaQueryWrapper<>();
        o.eq(Class::getCode1, code);
        Class aClass = classMapper.selectOne(o);

        if (aClass==null){
            throw new RuntimeException(MessageConstant.CODE_FAILED);
        }
        if (aClass.getTeacherId()!=null){
            throw new RuntimeException(MessageConstant.ADDCLASS_FAILED);
        }
        Teacher teacher1 = new Teacher();
        teacher1.setId(BaseContext.getCurrentId());
        teacher1.setPhone(teacher.getPhone());
        teacher1.setName(teacher.getName());
        teacher1.setKindergartenName(teacher.getKindergartenName());
        teacherMapper.insert(teacher1);

        aClass.setTeacherId(teacher1.getId());
        classMapper.updateById(aClass);
    }
}




